package com.yaodian100.ec.outlet.service.remote;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;

import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.core.common.service.BaseManager;
import com.yaodian100.core.mail.entity.Mail;
import com.yaodian100.core.mail.service.MailService;
import com.yaodian100.ec.common.EcConstant;
import com.yaodian100.ec.outlet.service.OutletService;
import com.yaodian100.ec.shelf.entity.ShelfTuangou;
import com.yaodian100.ec.shelf.service.ShelfTuangouService;

/**
 * 
 * @author scott.su
 * 
 */
public class RemoteOutletServiceImpl implements RemoteOutletService {

	@Autowired
	private OutletService outletService;

	@Autowired
	private ShelfTuangouService shelfTuangouService;

	@Autowired
	private BaseManager baseMgr;

	@Autowired
	private MailService mailService;

	@Override
	public boolean generateOutletList(String date) throws ParseException, CoreException, IOException {
		try {
			System.out.println("RemoteOutletServiceImpl generateOutletList:{}" + date + "; " + new Date());
			return outletService.generateOutletList(date);
		} catch (Exception e) {
			System.out.println("RemoteOutletServiceImpl generateOutletList exception:{}" + e.getMessage());
			return false;
		}
	}

	@Override
	public boolean generateOutletCalendar(String date) throws ParseException, CoreException, IOException {
		try {
			System.out.println("RemoteOutletServiceImpl generateOutletList:{}" + date + "; " + new Date());
			return outletService.generateOutletCalendar(date);
		} catch (Exception e) {
			System.out.println("RemoteOutletServiceImpl generateOutletList exception:{}" + e.getMessage());
			return false;
		}
	}

	@Override
	public boolean generateActivityList() throws ParseException, CoreException, IOException {
		try {
			System.out.println("RemoteOutletServiceImpl generateActivityList:{}");
			return shelfTuangouService.generateActivityList();
		} catch (Exception e) {
			System.out.println("RemoteOutletServiceImpl generateActivityList exception:{}" + e.getMessage());
			return false;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.outlet.service.remote.RemoteOutletService#sendExpiredTuangouMail()
	 */
	@Override
	public boolean sendExpiredTuangouMail() throws ParseException, CoreException, IOException {
		try {
			// 找已審核狀態，且今日到期的團購
			System.out.println("RemoteOutletServiceImpl sendExpiredTuangouMail:{}");
			String today = EcConstant.PATTERN_DATE_FORMAT.format(new Date());
			Object[] objs = baseMgr.queryByHql(
					"from ShelfTuangou where status='SHF500' and to_char(endDate,'yyyy-MM-dd') = '" + today + "'",
					new Object[] {});
			if (null != objs && objs.length > 0) {
				ShelfTuangou st = null;
				StringBuffer sb = new StringBuffer();
				for (Object obj : objs) {
					st = (ShelfTuangou) obj;
					sb.append("<p>团购编号：").append(st.getTuangouId()).append(",商品编号：").append(st.getProductId()).append(",团购名称：")
							.append(st.getPromoteName()).append("</p>");
				}
				Mail mail = new Mail("团购即将到期通知信", sb.toString(), "yaotuangou@yaodian100.com", "");
				mailService.save(mail);
				sb.setLength(0);
			}
		} catch (Exception e) {
			System.out.println("RemoteOutletServiceImpl sendExpiredTuangouMail exception:{}" + e.getMessage());
			return false;
		}
		return true;
	}
}
